import {getAllCustomerApi} from "@/api/customer"
import {getAllPeopleApi} from "@/api/customer/people";
import {getFileLinkApi} from "@/api/system/base";
import {getCustomerEnvApi} from "@/api/customer/env";

export const getAllCustomer = async () => {
  try {
    const res = await getAllCustomerApi()
    const customer = res.data.list.map((item) => {
      return {
        value: item.id,
        label: String(item.name)
      }
    })
    return customer
  } catch (error) {
    console.log(error)
  }
}

export const getCustomerEnv = async (customerId: string | number) => {
  try {
    const res = await getCustomerEnvApi({customerId})

    const Envs = res.data.list.map(item => ({ value: item.id, label: `${item.env}【${item.version}】` }));
    // Envs 就是包含 value 和 label 的对象数组
    return Envs;
  } catch (error) {
    console.log(error)
  }
}

export const getAllPeople = async () => {
  try {
    const res = await getAllPeopleApi()
    let job
    const people = res.data.list.map((item) => {
      switch (item.jobTitle) {
        case 1:
          job = "交付"
          break
        case 2:
          job = "销售"
          break
        case 3:
          job = "研发"
          break
        case 4:
          job = "售后"
          break
        case 5:
          job = "运维"
          break
        case 6:
          job = "产品"
          break
        case 7:
          job = "售前"
          break
        default:
          job = "N/A"
      }
      return {
        value: item.id,
        label: String(item.name) + ` 【${job}】`
      }
    })
    return people
  } catch (error) {
    console.log(error)
  }
}


export const downloadFile = async (id: number) => {
  try {
    const res = await getFileLinkApi(id, "file")
    // @ts-ignore
    const downloadUrl = res.data
    // 创建一个隐藏的 <a> 标签来触发下载
    const link = document.createElement('a')
    link.href = downloadUrl
    link.style.display = 'none'
    document.body.appendChild(link)
    // 触发点击事件并自动下载
    link.click();

    // 移除 <a> 标签
    document.body.removeChild(link);
  } catch (error) {
    console.log("下载文件失败: ", error)
  }
}
